<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\daikuan\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use app\user\model\UserModel;
use app\daikuan\model\LogisticsModel;
use app\user\model\UserLevelModel;
use app\daikuan\model\AuthrecordModel;
use app\daikuan\model\DriverrecordModel;
use app\daikuan\model\RoleUserModel;
use app\daikuan\model\DriverInterestModel;
use think\Db;
/**
 * Description of MyDriverController
 *
 * @author PVer
 */
class MyDriverController extends AdminBaseController {

    public function renzheng() {
        $id = cmf_get_current_admin_id();
        $map = array();
        $map["a.id"] = $id;
        $userModel = new UserModel();
        $info = db('user a ')->join(array(array('dk_driverrecord b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_logistics c', 'a.logistics=c.Id', 'LEFT')
                    , array('dk_authrecord d ', 'a.id=d.UserId', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,c.LogisticsName,a.number,a.dk_status,d.Img1,d.Img2,d.Img3,d.Img4,b.Img1 Img5,b.Img2 Img6,b.Img3 Img7,b.Img4 Img8')
                ->where($map)
                ->find();
        $userLevelModel = new UserLevelModel();

        $loglist = $userModel->where("user_category=2")->select();
        $this->assign('log_list', $loglist);
        $this->assign('info', $info);

        return $this->fetch();
    }

    //put your code here
    public function index() {
        $map = array();
        //        table排序
        $order = array();
        $order_type = input('order') ?? 'asc';
        $order_name = input('order_name') ?? 'a.can_loan_amount';
        if (empty($order_name)) {
            $order_name = 'a.can_loan_amount';
        }
        $order = "$order_name $order_type";
        $name = input('name');
        if (!empty($name)) {
            $map['a.user_nickname|a.mobile|a.id'] = ['like', '%' . $name . '%'];
            // $condition = "(a.user_nickname like '%$name%' or a.mobile like '%$name%' or a.id like '%$name%') ";
        }
        $map['user_category'] = 1;
        $map['a.logistics'] = cmf_get_current_admin_id();
        $dk_status = input('dk_status', -1, 'intval');
        if ($dk_status >= 0) {
            $map['a.dk_status'] = $dk_status;
        }
        $userModel = new UserModel();
        $list = db('user a ')->join(array(array('dk_driverrecord b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_logistics c', 'a.logistics=c.Id', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,c.LogisticsName,a.number,a.dk_status,(select count(*) from dk_driver_loan where DriverId=a.id and Status in (3,4)) isDaikuan,a.user_level,a.can_loan,a.can_loan_amount')
                ->where($map)
                ->order($order)
                ->paginate(10, false, ['query' => request()->param(),]);
        $page = $list->render();
        $this->assign('order', $order_type);
        $this->assign('dk_status', $dk_status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function loanlist() {
        $map = array();
        //        table排序
        $order = array();
        $order_type = input('order') ?? 'asc';
        $order_name = input('order_name') ?? 'a.ApplyAmount';
        if (empty($order_name)) {
            $order_name = 'a.ApplyAmount';
        }
        $order = "$order_name $order_type";

        $map['b.user_category'] = 1;
        $map['b.dk_status'] = 1;
        $status = input('status', -1, 'intval');
        if ($status >= 0) {
            $map['a.Status'] = $status;
        }
        $map["b.logistics"] = cmf_get_current_admin_id();
        $name = input('name');
        if (!empty($name)) {
            $map['user_nickname'] = array('like', "%$name%");
        }

        $list = db('driver_loan a')->join(array(array('dk_user b', 'a.DriverId=b.id ', 'LEFT')
                    , array('dk_user c', 'b.logistics=c.id', 'LEFT')
                ))
                ->field('a.*,b.user_nickname,b.mobile,b.create_time,b.number,c.user_nickname LogisticsName')
                ->order($order)
                ->where($map)
                ->paginate(10, false, ['query' => request()->param(),]);
        $page = $list->render();
        $this->assign('order', $order_type);
        $this->assign('Status', $status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function queren() {
        $driverInterestModel = new DriverInterestModel();
        $interest = $driverInterestModel->find();
        $this->assign("interest", $interest);
        $id = input('id');
        $map = array();
        $map['a.Id'] = $id;
        $map['b.dk_status'] = 1;
        $info = db('driver_loan a')->join(array(array('dk_user b', 'a.DriverId=b.id ', 'LEFT')
                    , array('dk_user c', 'b.logistics=c.id', 'LEFT')
                ))
                ->field('a.*,b.user_nickname,b.mobile,b.create_time,b.number,c.user_nickname LogisticsName')
                ->where($map)
                ->find();

        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function editPost() {
        if ($this->request->isPost()) {
            $id = input('Id');
            $data = input('post.');
            $result = false;
            if (isset($data['audit'])) {
                //发送短信
                $loan = Db::name('DriverLoan')->where('Id',$id)->find();
                $user=Db::name('user')->where('id',$loan['DriverId'])->find();
                $nickname= $user['user_nickname'];
                $tpl_value = "#name#=$nickname&#code#=$id";
                $flag = sendcode($user['mobile'],'91658',$tpl_value);

                $arr = array();
                $arr["Status"] = 1;
                $result = model('DriverLoan')->save($arr, ['Id' => $id]);
            } elseif (isset($data['refuse'])) {
                $arr = array();
                $arr["Status"] = 2;
                $result = model('DriverLoan')->save($arr, ['Id' => $id]);
            }
            if ($result !== false) {
                $this->success("操作成功", url("loanlist"));
            } else {
                $this->error("操作失败");
            }
        }
    }

    public function renzhengpost() {
        if ($this->request->isPost()) {

            $data = $this->request->post();
            $id = $data['id'];
            $userModel = new UserModel();
            $user = array();
            $user['mobile'] = $data['mobile'];
            $user['number'] = $data['number'];
            $user['logistics'] = $data['logistics'];
            $user['user_category'] = 1;
            $user['user_type'] = 4;
            $user['dk_status'] = 3;
            $result_1 = $userModel->save($user, 'id=' . $data['id']);
            if ($result_1 !== false) {
                $authrecordModel = new AuthrecordModel();
                $arr1 = array();
                $arr1['UserId'] = $id;
                $arr1['Img1'] = $data['imgid1'];
                $arr1['Img2'] = $data['imgid2'];
                $arr1['Img3'] = $data['imgid3'];
                $arr1['Img4'] = $data['imgid4'];
                $res1 = $authrecordModel->where("UserId=$id")->select();
                if (count($res1) > 0) {
                    $result_2 = $authrecordModel->save($arr1, "UserId=$id");
                } else {
                    $result_2 = $authrecordModel->save($arr1);
                }
                $driverrecordModel = new DriverrecordModel();
                $arr = array();
                $arr['UserId'] = $data['id'];
                $arr['Img1'] = $data['imgid5'];
                $arr['Img2'] = $data['imgid6'];
                $arr['Img3'] = $data['imgid7'];
                $arr['Img4'] = $data['imgid8'];

                $res2 = $driverrecordModel->where("UserId=$id")->select();
                if (count($res2) > 0) {
                    $result_3 = $driverrecordModel->save($arr, "UserId=$id");
                } else {
                    $result_3 = $driverrecordModel->save($arr);
                }
                if ($result_1 !== false && $result_2 !== false && $result_3 !== false) {
                    $this->success("操作成功", url('renzheng'));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error("请求错误1");
            }
        } else {
            $this->error("请求错误2");
            return $this->fetch();
        }
    }

}
